package site.yunnong.atvris.service.impl;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.dto.UserInterestTagBatchParam;
import site.yunnong.atvris.dao.domain.UserInterestTag;
import site.yunnong.atvris.dao.domain.UserInterestTagExample;
import site.yunnong.atvris.dao.mapper.UserInterestTagMapper;
import site.yunnong.atvris.service.UserInterestTagService;
import site.yunnong.atvris.common.dto.UserInterestTagParam;
import site.yunnong.atvris.service.helper.OperatorHelper;
import site.yunnong.atvris.service.vo.UserInterestTagVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 一个user只能有5个兴趣标签
 *
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:37
 */
@Service
public class UserInterestTagServiceImpl implements UserInterestTagService {

    @Resource
    private UserInterestTagMapper mapper;

    @Override
    public List<UserInterestTagVO> getUserInterestTags(Long uuid) {
        UserInterestTagExample example = new UserInterestTagExample();
        example.createCriteria().andUuidEqualTo(uuid);
        List<UserInterestTag> userInterestTags = mapper.selectByExample(example);
        return Optional.of(userInterestTags).orElse(Lists.newArrayList()).stream().map(this::buildUserInterestTagVO).collect(Collectors.toList());
    }

    @Override
    public int save(UserInterestTagBatchParam batchParam) {
        List<UserInterestTagParam> params = batchParam.getParamList();
        if (CollectionUtils.isEmpty(params)) {
            return 0;
        }
        Long uuid = OperatorHelper.getLoginUuid();
        // 先清除已有的
        deleteByUuid(uuid);
        for (UserInterestTagParam param : params) {
            UserInterestTag userInterestTag = new UserInterestTag();
            userInterestTag.setUuid(uuid);
            userInterestTag.setTid(param.getId());
            userInterestTag.setName(param.getName());
            mapper.insertSelective(userInterestTag);
        }
        return 1;
    }

    @Override
    public int insert(UserInterestTagParam param) {
        UserInterestTag userInterestTag = new UserInterestTag();
        Long uuid = OperatorHelper.getLoginUuid();
        userInterestTag.setUuid(uuid);
        userInterestTag.setTid(param.getId());
        userInterestTag.setName(param.getName());
        return mapper.insertSelective(userInterestTag);
    }

    @Override
    public int deleteByTid(Long tid) {
        Long uuid = OperatorHelper.getLoginUuid();
        UserInterestTagExample example = new UserInterestTagExample();
        example.createCriteria().andUuidEqualTo(uuid).andTidEqualTo(tid);
        return mapper.deleteByExample(example);
    }

    @Override
    public int deleteByUuid(Long uuid) {
        UserInterestTagExample example = new UserInterestTagExample();
        example.createCriteria().andUuidEqualTo(uuid);
        return mapper.deleteByExample(example);
    }

    private UserInterestTagVO buildUserInterestTagVO(UserInterestTag userInterestTag) {
        UserInterestTagVO vo = new UserInterestTagVO();
        BeanUtils.copyProperties(userInterestTag, vo);
        return vo;
    }
}

